package by.grsu.hdm;

import java.util.ArrayList;
import java.util.List;

import by.grsu.hdm.devices.MediaCenter;
import by.grsu.hdm.devices.NetworkMonitor;
import by.grsu.hdm.devices.UsbFlashDrive;
import by.grsu.hdm.devices.WashingMachine;
import by.grsu.hdm.exceptions.InvalidFormatException;
import by.grsu.hdm.media.Audio;
import by.grsu.hdm.media.Video;
import by.grsu.hdm.model.IDevice;
import by.grsu.hdm.model.IHomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.reporting.TextReportBuilder;
import by.grsu.hdm.reporting.TrafficReportData;

public class HomeDeviceManagerStarter {

	public static void main(String[] args) {
		
		List<IHomeDevice> devices = new ArrayList<IHomeDevice>();
		
		MediaCenter media = new MediaCenter("MEDIA#132456");
		media.getUsbPort().connectDevice(new UsbFlashDrive("FLASH-4GB"));
		
		devices.add(media);
		devices.add(new WashingMachine("WASHING#45"));
		
		// turn all devices on..
		for (IHomeDevice device : devices) {
				device.turnOn();
				System.out.println("Device with id "+device.getDeviceId()+" connected");
		}
		
		// check for connected devices
		for (IDevice device : devices) {
			if (device instanceof IUsbSupport) {
				IUsbPort usb = ((IUsbSupport)device).getUsbPort();
				if (usb.hasConnectedDevice()) {
					IDevice usbDevice = usb.getConnectedDevice();
					
					if (usbDevice != null)
					System.out.println(usbDevice.getDeviceId() + " is connected to " + device.getDeviceId());
				}
			}
		}
		
		
		// register devices for network monitoring
		NetworkMonitor monitor = new NetworkMonitor();
		
		for (IDevice device : devices) {
			if (device instanceof INetworkAvailable) {
				monitor.registerDevice((INetworkAvailable)device);
			}
		}
		
		
		
		
		TrafficReportData reportData = monitor.getNetworkStatistics();
		
		TextReportBuilder text = new TextReportBuilder(reportData);
		String report = text.buildReport();
		
		System.out.println(report);
		
		
		Video film1 = new Video("MP4", "1h5m10s", "Alexander");
		Audio song1 = new Audio("MP3", "5m43s", "ALexander");
		Audio song2 = new Audio("MPG", "7s", "Alexander");
		
		try
		{
			media.playAudio(song1);
		}catch(InvalidFormatException e)
		{
			
		}
		
		try
		{
			media.playVideo(film1);
		}catch(InvalidFormatException e)
		{
			
		}
		
		try
		{
			media.playAudio(song2);
		}catch(InvalidFormatException e)
		{
			System.out.println("Invalid song format "+song2.getFormat());
		}
		
		
		// turn all devices off..
		for (IHomeDevice device : devices) {
			device.turnOff();
		}
	}

}
